package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.CheckException;
import com.example.springboot.entity.CheckRoom;
import com.example.springboot.entity.Student;
import com.example.springboot.service.CheckExceptionService;
import com.example.springboot.service.CheckRoomService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/checkexception")
public class CheckExceptionController {


    @Resource
    private CheckExceptionService checkExceptionService;

    /**
     * 添加学生查寝信息
     */
    @PostMapping("/addcheckexception")
    public Result<?> add(@RequestBody CheckException checkException) {
        int i = checkExceptionService.addCheckException(checkException);
//        判断是否归寝异常
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "添加失败");
        }

    }


    /**
     * 查找归寝记录
     */
    @GetMapping("/find")
    public Result<?> findPage(@RequestParam(defaultValue = "0") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        PageInfo list = checkExceptionService.find(pageNum, pageSize, search);
        if (list != null) {
            return Result.success(list);
        } else {
            return Result.error("-1", "查询失败");
        }
    }

    /**
     * 更新状态
     * @param id
     * @return
     */
    @GetMapping("/updateState")
    public Result<?> updateState(@RequestParam(defaultValue = "") Integer id) {
        int i = checkExceptionService.updateStatu(id);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "更新失败");
        }
    }

    /**
     * 记录异常数
     */
    @GetMapping("/exceptionNum")
    public Result<?> exceptionNum() {
        int num = checkExceptionService.exceptionNum();
        if (num > 0) {
            return Result.success(num);
        } else {
            return Result.error("-1", "查询失败");
        }
    }


}
